package com.htjx.mall.controllers.converters;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

public class StringToDateConverter implements Converter<String, Date> {

	@Override
	public Date convert(String source) {
		if (!StringUtils.hasText(source)) {
			return null;
		} else {
			try {
				Date date = DateUtils.parseDate(source, new String[] { "yyyy/MM/dd", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd hh:mm", "yyyyMMdd" });
				return date;
			} catch (ParseException e) {
				throw new IllegalArgumentException("Could not parse date: " + e.getMessage(), e);
			}
		}
	}

}
